﻿/*
Copyright (c) 2016 Robert Gaffney
Licensed under the MIT License:

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 
documentation files (the "Software"), to deal in the Software without restriction, including without limitation 
the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and 
to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE 
WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Security.Cryptography;

namespace Kemper_Duplicate_Finder
{
    class MidiTag
    {
        public uint tagSize;
        public byte manufacturer;
        public byte device;
        public uint size;
        public byte[] type;
        public string typeString;
        public byte[] id;
        public string idString;
        public byte[] fullTag;
        public byte[] data;
        public string hash;

        public MidiTag(byte[] fullData)
        {
            int len = fullData.Length;
            int first = 0;
            this.typeString = "";
            this.idString = "";
            if (fullData[0] == 0xF0)
            {
                len--;
                first++;
            }
            if (fullData[fullData.Length - 1] == 0xF7)
            {
                len--;
            }

            if (fullData[first] == 0x00 && fullData[first + 1] == 0xF0)
            {
                len -= 2;
                first += 2;
            }

            this.fullTag = new byte[len];
            Array.Copy(fullData, first, this.fullTag, 0, len);

            uint i = readSize(0);
            if (i != 0)
            {
                i = readManufacturer(i);
                i = readDevice(i);
                i = readType(i);
                i = readID(i);
                readData(i);
                if (this.typeString.Equals("Profile"))
                {
                    genHash();
                }
            }
        }

        public uint readSize(uint start)
        {
            byte[] buffer = new byte[2];
            uint i = 0;

            try
            {
                while (this.fullTag[start] != 0x00) // terminates tag size - set the tag size
                {
                    buffer[i] = fullTag[start];
                    i++;
                    start++;
                }
            }
            catch (IndexOutOfRangeException ex)
            {
                this.size = 0;
                Console.WriteLine("Error on tag: " + BitConverter.ToString(this.fullTag) + "\r\n" + ex.ToString());
                return 0;
            }

            if (start > 0)
            {
                byte[] temp = new byte[i];
                Array.Copy(buffer, 0, temp, 0, i);
                if (temp.Length == 1)
                {
                    this.size = (uint)temp[0];
                }
                else
                {
                    this.size = BitConverter.ToUInt16(temp, 0);
                }
                //Console.WriteLine("size: " + this.size);
                return start + 1;
            }
            this.size = 0;
            return 1;
        }

        public uint readManufacturer(uint start)
        {
            this.manufacturer = fullTag[start];
            //Console.WriteLine("manufacturer: " + this.manufacturer.ToString("X"));
            return start + 1;
        }
        public uint readDevice(uint start)
        {
            this.device = fullTag[start];
            //Console.WriteLine("device: " + this.device.ToString("X"));
            return start + 1;
        }

        public uint readType(uint start)
        {
            this.type = new byte[2];
            Array.Copy(fullTag, start + 1, this.type, 0, 2);
            string value = BitConverter.ToString(this.type).Replace("-", string.Empty);
            //Console.WriteLine("Type: " + value);
            switch (value)
            {
                case "0002":
                    this.typeString = "Setting";
                    break;
                case "0003":
                    this.typeString = "Tag";
                    break;
                case "0004":
                    this.typeString = "Profile";
                    break;
                default:
                    this.typeString = "Unknown (" + value + ")";
                    break;
            }
            //Console.WriteLine("type: " + this.type);
            return start + 3;
        }

        public uint readID(uint start)
        {
            this.id = new byte[2];
            Array.Copy(fullTag, start + 1, id, 0, 2);
            string value = BitConverter.ToString(this.id).Replace("-", string.Empty);
            //Console.WriteLine("ID: " + value);
            switch (typeString)
            {
                case "Profile":
                    this.setProfileIDString(value);
                    break;
                case "Tag":
                    this.setTagIDString(value);
                    break;
                case "Setting":
                    this.idString = "?" + value;
                    break;
                default:
                    this.idString = "?" + value;
                    break;
            }

            //Console.WriteLine("id: " + this.id);
            return start + 3;
        }

        private void setProfileIDString(string value)
        {
            switch (value)
            {
                case "0010":
                    this.idString = "Amp10";
                    break;
                case "0011":
                    this.idString = "Amp11";
                    break;
                case "0012":
                    this.idString = "Amp12";
                    break;
                case "0020":
                    this.idString = "Cab20";
                    break;
                case "0021":
                    this.idString = "Amp21";
                    break;
                case "0022":
                    this.idString = "Amp22";
                    break;
                case "0030":
                    this.idString = "Cab30";
                    break;
                case "0031":
                    this.idString = "Cab31";
                    break;
                case "0032":
                    this.idString = "Cab32";
                    break;
                case "0040":
                    this.idString = "Cab40";
                    break;
                case "0041":
                    this.idString = "Cab41";
                    break;
                case "0042":
                    this.idString = "Cab42";
                    break;
                default:
                    this.idString = "?" + value;
                    break;
            }
        }

        private void setTagIDString(string value)
        {
            switch (value)
            {
                case "0001":
                    this.idString = "Rig Name";
                    break;
                case "0002":
                    this.idString = "Rig Author";
                    break;
                case "0003":
                    this.idString = "Rig Creation Date";
                    break;
                case "0004":
                    this.idString = "Rig Comment";
                    break;
                case "0005":
                    this.idString = "Favorite Rig Owner";
                    break;
                case "0006":
                    this.idString = "Rig Rating and By";
                    break;
                case "0010":
                    this.idString = "Amp Name";
                    break;
                case "0011":
                    this.idString = "Amp Author";
                    break;
                case "0012":
                    this.idString = "Amp Creation Date";
                    break;
                case "0013":
                    this.idString = "Amp Creation Time";
                    break;
                case "0014":
                    this.idString = "Amp Profiling Location";
                    break;
                case "0015":
                    this.idString = "Source Amp";
                    break;
                case "0016":
                    this.idString = "Amp Comment";
                    break;
                case "0017":
                    this.idString = "Amp Artist";
                    break;
                case "0018":
                    this.idString = "Amp Model";
                    break;
                case "0019":
                    this.idString = "Amp Channel";
                    break;
                case "001A":
                    this.idString = "Pickup Type";
                    break;
                case "001B":
                    this.idString = "Amp Year";
                    break;
                case "0020":
                    this.idString = "Cab Name";
                    break;
                case "0021":
                    this.idString = "Cab Author";
                    break;
                case "0022":
                    this.idString = "Cab Creation Date";
                    break;
                case "0023":
                    this.idString = "Cab Creation Time";
                    break;
                case "0024":
                    this.idString = "Cab Profiling Location";
                    break;
                case "0025":
                    this.idString = "Cab Model";
                    break;
                case "0026":
                    this.idString = "Mic Model";
                    break;
                case "0027":
                    this.idString = "Cab Comment";
                    break;
                case "0028":
                    this.idString = "Mic Position";
                    break;
                case "0029":
                    this.idString = "Speaker Config";
                    break;
                case "002A":
                    this.idString = "Source Cab";
                    break;
                case "002B":
                    this.idString = "Cab Artist";
                    break;
                case "002C":
                    this.idString = "Speaker Manufacturer";
                    break;
                case "002D":
                    this.idString = "Speaker Model";
                    break;
                default:
                    this.idString = "?" + value;
                    break;
            }
        }

        public void readData(uint start)
        {
            uint len = (uint)fullTag.Length - start;
            this.data = new byte[len];
            Array.Copy(fullTag, start, data, 0, len);
            //Console.WriteLine("data: " + this.data);
        }

        public void genHash()
        {
            MD5 md5Hash = MD5.Create();
            byte[] data = md5Hash.ComputeHash(this.fullTag);
            StringBuilder sBuilder = new StringBuilder();
            for (int i = 0; i < data.Length; i++)
            {
                sBuilder.Append(data[i].ToString("x2"));
            }
            this.hash = sBuilder.ToString();
        }
    }
}
